package com.test.learnkotlin

/**
 *Author: chinadragon
 *Time: 2024/3/13 09:19
 */


abstract class SomeClass<T> {
    abstract fun execute() : T
}

class SomeImplementation : SomeClass<String>() {
    override fun execute(): String = "Test"
}

class SomeImplementation1 : SomeClass<String>(){
    override fun execute(): String {
        return "测试"
    }

}

class OtherImplementation : SomeClass<Int>() {
    override fun execute(): Int = 42
}

class OtherImplementation2 : SomeClass<Int>(){
    override fun execute(): Int {
        return 20
    }

}

object Runner {
    inline fun <reified S: SomeClass<T>, T> run() : T {
        return S::class.java.getDeclaredConstructor().newInstance().execute()
    }
}

fun main() {
    //类型参数的下划线操作符仅在Kotlin 1.7.0 ，查看Kotlin 1.7.0 的新特性
    //https://kotlinlang.org/docs/whatsnew17.html#underscore-operator-for-type-arguments


    // T is inferred as String because SomeImplementation derives from SomeClass<String>
    //T被推断为String，因为SomeImplementation是SomeClass<String>的派生

//    val s = Runner.run<SomeImplementation, _>() //这里放开报错，需要将Kotlin 升级到1.7.0 之后
//    assert(s == "Test")
//    println("s = $s")

    // T is inferred as Int because OtherImplementation derives from SomeClass<Int>

//    val n = Runner.run<OtherImplementation, _>() // 这里放开报错，需要将Kotlin 升级到1.7.0 之后
//    assert(n == 42)
//    println("n = $n")

}